package sisi.reporting;

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.EntityManager;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ComboitemRenderer;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listfooter;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import sisi.General;

/**
 * ShortDayBookWindow.java - Controller class for <i>Short Day Book window</i>.
 * 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 01/04/2015
 * 
 */

public class ShortDayBookWindow extends Window implements
		org.zkoss.zk.ui.ext.AfterCompose {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unused")
	private Window windowShortdaybook;
	private Combobox cbRef;
	private Listbox boxInvoices;
	private Listfooter lfTotInvoice;

	public void afterCompose() {
		// wire variables
		ConventionWires.wireVariables(this, this);
		// auto forward
		ConventionWires.addForwards(this, this);
	}

	// inizio apertura finestra windowshortdaybook
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void onCreate$windowShortdaybook(Event event) {

		// carico lista riferimenti ordini
		List orders = getListRef();

		// aggiungo lista al model list
		ListModelList listModelList = new ListModelList();
		listModelList.addAll(orders);

		// aggiungo il modelo (model list) al combobox
		cbRef.setModel(listModelList);
		
		// indico al combobox come verrà visualizzato
		cbRef.setItemRenderer(new OrderItemRenderer());

	}

	@SuppressWarnings("rawtypes")
	public List getListRef() {

		EntityManager em = new sisi.MyEmf().getEntityManager();
		try {
			String cQuery = "";

			cQuery += "select custinvoice.order_ FROM custinvoice WHERE order_ IS NOT NULL GROUP BY custinvoice.order_ ORDER BY custinvoice.order_ DESC";

			javax.persistence.Query q = em.createNativeQuery(cQuery);

			List val = q.getResultList();
			return val;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("rawtypes")
	private class OrderItemRenderer implements ComboitemRenderer {

		@Override
		public void render(Comboitem item, Object data, int index)
				throws Exception {

			Integer orderNumber = (Integer) data;

			item.setLabel("" + orderNumber);
			// item.setDescription(su.getSupname());
		}

	}

	public void onOK$cbRef() {
		selectedOrder();
	}

	public void onSelect$cbRef() {
		selectedOrder();

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void selectedOrder() {
		String numOrder = cbRef.getValue();
		Integer indice = cbRef.getSelectedIndex();
		//General.showNotify("" + indice + " - " + numOrder, null);

		if (indice < 0 || numOrder == null || numOrder.isEmpty()) {
			General.showNotify("Select a valid order!!", null, 4000, "error");
			return;
		}

		List listaCustInvoice = getListCustinvoice( Integer.valueOf(numOrder) );
		Double totInvoice = 0.00;
		for (Object object : listaCustInvoice) {
			Object[] ogg = (Object[])object;
			Double totalinvoice = (Double)ogg[7];
			totInvoice += (totalinvoice!=null ? totalinvoice : 0 );			
		}
		
		ListModelList listModelList = new ListModelList();
		listModelList.addAll(listaCustInvoice);
		boxInvoices.setModel(listModelList);
		boxInvoices.setItemRenderer(new CustinvoicesItemRenderer());
		
		lfTotInvoice.setLabel( ""+General.decimalFormat("###,##0.00", totInvoice ) );
		
		
		

	}

	@SuppressWarnings("rawtypes")
	public class CustinvoicesItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			
			Object[] ogg = (Object[])data;
			Integer custid = (Integer)ogg[0];
			String custname = (String)ogg[1];
			Integer order = (Integer)ogg[2];
			Integer custnuminvoice = (Integer)ogg[3];
			Timestamp invoicedt = (java.sql.Timestamp)ogg[4];
			Timestamp deliverydt = (java.sql.Timestamp)ogg[5];
			String stat = (String)ogg[6];
			Double totalinvoice = (Double)ogg[7];
			
			new Listcell(""+custid).setParent(li);
			new Listcell(""+custname).setParent(li);
			new Listcell(""+order).setParent(li);
			new Listcell(""+custnuminvoice).setParent(li);
			new Listcell( General.formatoFecha( invoicedt) ).setParent(li);
			new Listcell( General.formatoFecha( deliverydt ) ).setParent(li);
			
			
			Textbox tbStatus =  new Textbox(""+stat);
			tbStatus.setWidth("40px"); 
			tbStatus.setReadonly(true);
			tbStatus.setStyle("text-align: center; color:green;");
			if (stat!=null && stat.equalsIgnoreCase("PD")){
				tbStatus.setStyle("text-align: center; color:red;");
			}
			
			Listcell lcStatus = new Listcell();
			lcStatus.appendChild(tbStatus);
			lcStatus.setParent(li);
			//new Listcell( ""+stat ).setParent(li);
			
			new Listcell( ""+General.decimalFormat("###,##0.00", totalinvoice) ).setParent(li);
			
			//li.setAttribute("rigaBank", data);
			//ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}
	
	@SuppressWarnings("rawtypes")
	public List getListCustinvoice(Integer numOrder) {

		EntityManager em = new sisi.MyEmf().getEntityManager();
		try {
			String cQuery = "";

			cQuery += "select custinvoice.custid, custinvoice.custname, custinvoice.order_," +
					" custinvoice.custnuminvoice, custinvoice.invoicedt, custinvoice.deliverydt, " +
					" custinvoice.stat, custinvoice.totalinvoice FROM custinvoice WHERE custinvoice.order_ = "
					+ numOrder + " ORDER BY custinvoice.custnuminvoice ASC ";

			
			//System.out.println(cQuery);
			
			javax.persistence.Query q = em.createNativeQuery(cQuery);

			List val = q.getResultList();
			return val;
		} finally {
			em.close();
		}
	}

}
